//
//  UIView+Extension.swift
//  BookRead
//
//  Created by fox on 2017/5/11.
//  Copyright © 2016年 DZM. All rights reserved.
//

import Foundation
import UIKit

extension UIView{
    
    // MARK: -- 扩展方法
    
    func viewController() ->UIViewController? {
        
        //通过响应者链，取得此视图所在的视图控制器
        var next = self.next
        
        repeat{
            
            //判断响应者对象是否是视图控制器类型
            if next!.isKind(of: UIViewController.classForCoder()) {
                
                return next as? UIViewController
            }
            
            next = next!.next
            
        }while next != nil
        
        return nil
    }
    
    // MARK: -- 扩展属性使用
    
    public var x: CGFloat{
        get{
            return self.frame.origin.x
        }
        set{
            var r = self.frame
            r.origin.x = newValue
            self.frame = r
        }
    }
    
    public var y: CGFloat{
        get{
            return self.frame.origin.y
        }
        set{
            var r = self.frame
            r.origin.y = newValue
            self.frame = r
        }
    }
    
    public var width: CGFloat{
        get{
            return self.frame.size.width
        }
        set{
            var r = self.frame
            r.size.width = newValue
            self.frame = r
        }
    }
    public var height: CGFloat{
        get{
            return self.frame.size.height
        }
        set{
            var r = self.frame
            r.size.height = newValue
            self.frame = r
        }
    }
    
    
    public var origin: CGPoint{
        get{
            return self.frame.origin
        }
        set{
            self.x = newValue.x
            self.y = newValue.y
        }
    }
    
    public var size: CGSize{
        get{
            return self.frame.size
        }
        set{
            self.width = newValue.width
            self.height = newValue.height
        }
    }
    
    public var centerX : CGFloat{
        get{
            return self.center.x
        }
        set{
            self.center = CGPoint(x: newValue, y: self.center.y)
        }
    }
    
    public var centerY : CGFloat{
        get{
            return self.center.y
        }
        set{
            self.center = CGPoint(x: self.center.x, y: newValue)
        }
    }
    
    public var rightX: CGFloat{
        get{
            return self.x + self.width
        }
        set{
            var r = self.frame
            r.origin.x = newValue - frame.size.width
            self.frame = r
        }
    }

    public var bottomY: CGFloat{
        get{
            return self.y + self.height
        }
        set{
            var r = self.frame
            r.origin.y = newValue - frame.size.height
            self.frame = r
        }
    }
    

}


// MARK: - 判断一个View 是否在另一个View的视图层级里面
extension UIView {
    
    /// 判断 viewA(self) 在不在 viewB 的视图层级里面
    ///
    /// - Parameter view: viewB
    /// - Returns: true/false
    func isInViewHirerackyOf(view: UIView) -> Bool {
        
        var superview: UIView? = self
        while superview != nil {
            if superview === view {
                return true
            }
            superview = superview?.superview
        }
        return false
    }
}

extension UIView {
    func showActivity() {
        let activi =  UIActivityIndicatorView.init(activityIndicatorStyle:.white)
        self.addSubview(activi)
        activi.center = self.center
        activi.startAnimating()
        activi.color = UIColor.black

        activi.tag = 99
    }

    func dimissActivity() {
        for view in self.subviews {
            if view.tag == 99 {
                let activi = view as!UIActivityIndicatorView
                activi.stopAnimating()
                activi.removeFromSuperview()
            }
        }
    }
}

